﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ObjectManager;

namespace QuanLyDiem
{
    public partial class frmQLSinhVien : Form
    {
        private static frmQLSinhVien frm = null;

        private int HeightDataGridView = 0;

        private frmQLSinhVien()
        {
            InitializeComponent();
        }

        public static frmQLSinhVien Create()
        {
            if (frm == null)
            {
                frm = new frmQLSinhVien();
                return frm;
            }
            return frm;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (btnThem.Text == "Thêm")
            {
                txtMaSV.Text = "";
                btnThem.Text = "Lưu";
                btnSua.Text = "Hủy";
                btnXoa.Enabled = false;
            }
            else
            {
                switch (btnThem.Text)
                {
                    case "Lưu":
                        Them();
                        break;
                    case "Sửa":
                        Sua();
                        KhoiPhucButtom();
                        break;
                    default:
                        Xoa();
                        KhoiPhucButtom();
                        break;
                }
            }
        }

        private void Them()
        {
            SinhVien sv = new SinhVien();
            sv.ThongTin.Ten = txtTenSV.Text;
            sv.ThongTin.Lop = cmbLop.Text;
            sv.ThongTin.BoMon = cmbBoMon.Text;
            sv.ThongTin.QueQuan = txtQueQuan.Text;
            if (rdNam.Checked)
            {
                sv.ThongTin.GioiTinh = "Nam";
            }
            else
            {
                sv.ThongTin.GioiTinh = "Nữ";
            }
            sv.ThongTin.NgaySinh = dtpNgaySinh.Value;
            int check = sv.Add();
            if (check > 0)
            {
                MessageBox.Show("Đã thêm thành công", "Thông báo");
                DataTable dt = (DataTable)dgvSinhVien.DataSource;
                DataRow dr = dt.NewRow();
                dr[0] = sv.ThongTin.Ma;
                dr[1] = sv.ThongTin.Ten;
                dr[2] = sv.ThongTin.Lop;
                dr[3] = sv.ThongTin.BoMon;
                string ns = "";
                if (sv.ThongTin.NgaySinh.Day < 10)
                {
                    ns += "0" + sv.ThongTin.NgaySinh.Day.ToString();
                }
                else
                {
                    ns += sv.ThongTin.NgaySinh.Day.ToString();
                }
                if (sv.ThongTin.NgaySinh.Month < 10)
                {
                    ns += "0" + sv.ThongTin.NgaySinh.Month.ToString();
                }
                else
                {
                    ns += sv.ThongTin.NgaySinh.Month.ToString();
                }
                ns += sv.ThongTin.NgaySinh.Year.ToString();
                dr[4] = ns;
                if (rdNam.Checked)
                {
                    dr[5] = "Nam";
                }
                else
                {
                    dr[5] = "Nữ";
                }
                dr[6] = txtQueQuan.Text;
                dt.Rows.Add(dr);
                dgvSinhVien.DataSource = dt;
            }
        }

        private void Xoa()
        {
            SinhVien sv = new SinhVien();
            sv.ThongTin.Ma = dgvSinhVien.SelectedRows[0].Cells["cMaSV"].Value.ToString();
            int check = sv.Remove();
            if (check > 0)
            {
                MessageBox.Show("Đã xóa thành công", "Thông báo");
                dgvSinhVien.Rows.Remove(dgvSinhVien.SelectedRows[0]);
                BindingData();
            }
        }

        private void Sua()
        {
            SinhVien sv = new SinhVien();
            sv.ThongTin.Ma = dgvSinhVien.SelectedRows[0].Cells["cMaSV"].Value.ToString();
            sv.ThongTin.Ten = txtTenSV.Text;
            sv.ThongTin.Lop = cmbLop.Text;
            sv.ThongTin.BoMon = cmbBoMon.Text;
            if (rdNam.Checked)
            {
                sv.ThongTin.GioiTinh = "Nam";
            }
            else
            {
                sv.ThongTin.GioiTinh = "Nữ";
            }
            sv.ThongTin.NgaySinh = dtpNgaySinh.Value;
            sv.ThongTin.QueQuan = txtQueQuan.Text;
            int check = sv.Update();
            if (check > 0)
            {
                dgvSinhVien.SelectedRows[0].Cells["cTenSV"].Value = txtTenSV.Text;
                dgvSinhVien.SelectedRows[0].Cells["cLop"].Value = cmbLop.Text;
                dgvSinhVien.SelectedRows[0].Cells["cboMon"].Value = cmbBoMon.Text;
                dgvSinhVien.SelectedRows[0].Cells["cNgaySinh"].Value = dtpNgaySinh.Text;
                if (rdNam.Checked)
                {
                    dgvSinhVien.SelectedRows[0].Cells["cGioiTinh"].Value = "Nam";
                }
                else
                {
                    dgvSinhVien.SelectedRows[0].Cells["cGioiTinh"].Value = "Nữ";
                }
                dgvSinhVien.SelectedRows[0].Cells["cQueQuan"].Value = txtQueQuan.Text;
            }
        }
        private void BindingData()
        {
            if (dgvSinhVien.SelectedRows.Count > 0)
            {
                txtMaSV.Text = dgvSinhVien.SelectedRows[0].Cells[0].Value.ToString();
                txtTenSV.Text = dgvSinhVien.SelectedRows[0].Cells[1].Value.ToString();
                cmbLop.Text = dgvSinhVien.SelectedRows[0].Cells["cLop"].Value.ToString();
                dtpNgaySinh.Text = dgvSinhVien.SelectedRows[0].Cells["cNgaySinh"].Value.ToString();
                if (dgvSinhVien.SelectedRows[0].Cells["cGioiTinh"].Value.ToString() == "Nam")
                {
                    rdNam.Checked = true;
                }
                else
                {
                    rdNu.Checked = true;
                }
                cmbBoMon.Text = dgvSinhVien.SelectedRows[0].Cells["cBoMon"].Value.ToString();
                txtQueQuan.Text = dgvSinhVien.SelectedRows[0].Cells["cQueQuan"].Value.ToString();
            }
        }

        private void cmbBoMon_SelectedIndexChanged(object sender, EventArgs e)
        {
            //cmbLop.DataSource = Lop.ListName(cmbBoMon.Text);
        }

        private void frmQLSinhVien_Load(object sender, EventArgs e)
        {
            SinhVien sinhvien = new SinhVien();
            dgvSinhVien.DataSource = sinhvien.List();
            BoMon bomon = new BoMon();
            cmbBoMon.DataSource = bomon.ListName();
            if (!string.IsNullOrEmpty(cmbBoMon.Text))
            {
                //cmbLop.DataSource = Lop.ListName(cmbBoMon.Text);
            }
            if (dgvSinhVien.Rows.Count > 0)
            {
                BindingData();
            }
            this.HeightDataGridView = dgvSinhVien.Location.Y;
        }

        private void dgvSinhVien_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                txtMaSV.Text = dgvSinhVien.Rows[e.RowIndex].Cells[0].Value.ToString();
                txtTenSV.Text = dgvSinhVien.Rows[e.RowIndex].Cells[1].Value.ToString();
                cmbLop.Text = dgvSinhVien.Rows[e.RowIndex].Cells["cLop"].Value.ToString();
                dtpNgaySinh.Text = dgvSinhVien.Rows[e.RowIndex].Cells["cNgaySinh"].Value.ToString();
                if (dgvSinhVien.Rows[e.RowIndex].Cells["cGioiTinh"].Value.ToString() == "Nam")
                {
                    rdNam.Checked = true;
                }
                else
                {
                    rdNu.Checked = true;
                }
                cmbBoMon.Text = dgvSinhVien.Rows[e.RowIndex].Cells["cBoMon"].Value.ToString();
                txtQueQuan.Text = dgvSinhVien.Rows[e.RowIndex].Cells["cQueQuan"].Value.ToString();
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            btnThem.Text = "Xóa";
            btnSua.Text = "Hủy";
            btnXoa.Enabled = false;
        }

        private void KhoiPhucButtom()
        {
            btnThem.Text = "Thêm";
            btnSua.Text = "Sửa";
            btnXoa.Text = "Xóa";
            btnXoa.Enabled = true;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (btnSua.Text == "Hủy")
            {
                KhoiPhucButtom();
                BindingData();
            }
            else
            {
                btnThem.Text = "Sửa";
                btnXoa.Enabled = false;
                btnSua.Text = "Hủy";
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this.Size.Height.ToString() + " " + this.Height.ToString());
        }

        private void frmQLSinhVien_Resize(object sender, EventArgs e)
        {
            //if (this.Height > dgvSinhVien.Height + this.HeightDataGridView)
            //{
                dgvSinhVien.Height = this.Height - this.HeightDataGridView - 40;
            //}
        }

        private void frmQLSinhVien_FormClosing(object sender, FormClosingEventArgs e)
        {
            frm.Dispose();
            frm = null;
        }
    }
}
